﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CAD.BLL;
using CAD.DTO;

namespace WFASmartketing.PRESENTACION_TESORERIA
{
    public partial class DepositoCaja : Form
    {
        private FrmPrincipal principal;
        public DepositoCaja(FrmPrincipal principal)
        {
            InitializeComponent();
            this.principal = principal; 
            cbxcaja.DropDownStyle = ComboBoxStyle.DropDownList;
        }

        private void txtmonto_KeyPress(object sender, KeyPressEventArgs e)
        {
            UTIIIDADES.UTILIDADES.soloDecimanes(e,txtmonto);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void DepositoCaja_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'vendedorDS.vendedor_selectAll' table. You can move, or remove it, as needed.
            this.vendedor_selectAllTableAdapter.Fill(this.vendedorDS.vendedor_selectAll);
            // TODO: This line of code loads data into the 'cajaDS.caja_selectAll' table. You can move, or remove it, as needed.
            this.caja_selectAllTableAdapter.Fill(this.cajaDS.caja_selectAll);

        }
        private void limpiar() {
            txtmonto.Text = "";
            txtnrorecibo.Text = "";
            txtobservacion.Text = "";
            dtcefecha.Value = DateTime.Now;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            limpiar();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            double monto = 0;
            try
            {
                monto = Convert.ToDouble(txtmonto.Text);
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, "Debe ingresar un monto", "ERROR");
                return;
            }
            if (monto <= 0)
            {
                MessageBox.Show(this, "El monto debe ser mayor al 0", "ERROR");
                return;
            }
            DateTime fecha = dtcefecha.Value;
            int idcuenta = (int)cbxcaja.SelectedValue;
            string responsable = cbxresponsable.Text.ToString();
            string observacion = txtobservacion.Text;
            string nrorecibo = txtnrorecibo.Text;
            try
            {
                caja cuenta = cajaBLL.SelectByID(idcuenta);
                double montoActual1 = cuenta.Monto + monto;
                cajaBLL.updatemonto(cuenta.Id, montoActual1);
                int nroRetiro = depositoCajaBLL.insertar(idcuenta, principal.ObtenerLogueado().Id, DateTime.Now, fecha, responsable, nrorecibo, monto, observacion);
                MessageBox.Show(this, "Se registro el deposito correctamente, Nro deposito: " + nroRetiro, "DEPOSITO");
                limpiar();
                imprimirComprobante(nroRetiro);
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, "No se registro el deposito", "ERROR");
            }
        }
        private void imprimirComprobante(int pkCompra)
        {
            MessageBoxButtons buttons = MessageBoxButtons.YesNo;
            DialogResult result;
            result = MessageBox.Show(this, "Desea Imprimir el comprobante", "REGISTRO DE DATOS", buttons);
            if (result == DialogResult.Yes)
            {
                REPORTES.ComprobanteDepositoCajaWF compra = new REPORTES.ComprobanteDepositoCajaWF(pkCompra);
                compra.MdiParent = principal;
                compra.Show();
                return;
            }
        }
    }
}
